<?php

	namespace Phptricks\system\library\pagination;

	/**
	 * Description of pagination
	 *
	 * @author phptricks
	 */
	use Phptricks\system\core\library\library;

	class pagination implements library{

		public $_current_page = 1;
		public $_max_rows;
		public $_count_pager = 10;
		public $_count_rows = 20;
		public $_first_page;
		public $_last_page;
		public $_pages;
		public $_limit;
		private static $_instance = null;

		public static function getinstance(){
			if (self::$_instance === null) {
				self::$_instance = new \Phptricks\system\library\pagination\pagination();
			}
			return self::$_instance;
		}

		public function __construct(){
			$this->_current_page = 0;
			$this->_max_rows = 50;
			$this->_count_pager = 0;
			$this->_pages = array();
		}

		public function _set_pagination($row_count = 1, $current_page = 1, $max_rows = 20, $select_from = '5'){
			if (empty($row_count)) {
				$row_count = 1;
			}
			$this->_count_rows = $row_count;
			$current_page = intval($current_page);
			if (empty($current_page)) {
				$current_page = 1;
			}
			if (empty($max_rows)) {
				$max_rows = 20;
			}

			$count_pages = ceil($row_count / $max_rows);
			if ($current_page <= 1) {
				$current_page = 1;
			}
			$this->_current_page = $current_page;
			$this->_max_rows = $max_rows;
			$this->_count_pager = $count_pages;
			if ($this->_max_rows > 0) {
				for ($i = 1; $i < $this->_count_pager; $i++) {
					$this->_pages[$i] = $i;
				}
				$this->_pages[(\count($this->_pages) + 1)] = (\count($this->_pages) + 1);
			} else {
				$this->_pages = array('1' => '1');
			}
			$this->_first_page = 1;
			$this->_last_page = \count($this->_pages);
			$this->get_pagination($select_from);
			if ($current_page < 1) {
				$limit_from = $max_rows;
			} else {
				$limit_from = str_replace('-', '', (($current_page * $max_rows) - $max_rows));
			}

			$this->_limit = rtrim($limit_from . " , " . $max_rows, ',');
		}

		private function get_pagination($select_from = '1'){
			if ($this->_current_page > ceil($select_from / 2)) {
				$page_from = \ceil(str_replace('-', '', ($this->_current_page - ceil($select_from / 2))));
			} else {
				$page_from = 0;
			}
			$page_to = \ceil($this->_current_page + ceil($select_from / 2));
			$this->_pages = array_slice($this->_pages, $page_from, $select_from);
			return true;
		}

	}

?>
